package com.adtech.farmProducts.utils;

import android.os.Environment;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationQualityReport;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;

/**
 * Time:2020/4/30
 * Created by xiaocz
 * Describe
 */
public class ToSDcard {

    /**
     * 保存错误信息到文件中
     *
     * @return
     */
    public static void saveInfoToFile(String result) {
        BufferedWriter fw = null;
        //判断SD卡不存在或无法使用
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return;
        }
        File dir = new File(CrashHandler.PATH);
        if (!dir.exists()) {
            boolean isSuccess = dir.mkdir();
        }
        File file = new File(CrashHandler.PATH + "error.log");
        try {
            fw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true), "UTF-8")); // 指定编码格式，以免读取时中文字符异常
            fw.append("*104*");
            fw.append(CrashHandler.simpleDateFormat.format(new Date(System.currentTimeMillis())));
            fw.append("*" + result + "*");
            fw.newLine();
            fw.flush(); // 全部写入缓存中的内容
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getLocationString(AMapLocation location) {
        StringBuilder sb = new StringBuilder();
        if (null != location) {
            if (location.getErrorCode() == 0) {
                sb.append("成功" + "\n");
                sb.append("定位类型: " + location.getLocationType() + "\n");
                sb.append("坐标: " + location.getLongitude() + "," + location.getLatitude() + "\n");
                sb.append("精度: " + location.getAccuracy() + "米" + "\n");
                sb.append("提供者: " + location.getProvider() + "\n");
                sb.append("速度 : " + location.getSpeed() + "米/秒" + "\n");
                sb.append("星数: " + location.getSatellites() + "\n");
                sb.append("省: " + location.getProvince() + "\n");
                sb.append("市: " + location.getCity() + "\n");
                sb.append("城市编码 : " + location.getCityCode() + "\n");
                sb.append("区域码: " + location.getAdCode() + "\n");
                sb.append("地址:" + location.getAddress() + ":" + location.getDescription());
                sb.append("兴趣点:" + location.getPoiName());
            } else {
                //定位失败
                sb.append("失败" + "\n");
                sb.append("错误码:" + location.getErrorCode() + "\n");
                sb.append("错误信息:" + location.getErrorInfo() + "\n");
                sb.append("错误描述:" + location.getLocationDetail() + "\n");
            }
            sb.append("***定位质量报告***").append("\n");
            sb.append("* WIFI开关：").append(location.getLocationQualityReport().isWifiAble() ? "开启" : "关闭").append("\n");
            sb.append("* GPS状态：").append(getGPSStatusString(location.getLocationQualityReport().getGPSStatus())).append("\n");
            sb.append("* GPS星数：").append(location.getLocationQualityReport().getGPSSatellites()).append("\n");
            sb.append("****************").append("\n");
        } else {
            sb.append("失败" + "\n");
            sb.append("location=null");
        }
        return sb.toString();
    }

    private static String getGPSStatusString(int statusCode) {
        String str = "";
        switch (statusCode) {
            case AMapLocationQualityReport.GPS_STATUS_OK:
                str = "GPS状态正常";
                break;
            case AMapLocationQualityReport.GPS_STATUS_NOGPSPROVIDER:
                str = "手机中没有GPS Provider，无法进行GPS定位";
                break;
            case AMapLocationQualityReport.GPS_STATUS_OFF:
                str = "GPS关闭，建议开启GPS，提高定位质量";
                break;
            case AMapLocationQualityReport.GPS_STATUS_MODE_SAVING:
                str = "选择的定位模式中不包含GPS定位，建议选择包含GPS定位的模式，提高定位质量";
                break;
            case AMapLocationQualityReport.GPS_STATUS_NOGPSPERMISSION:
                str = "没有GPS定位权限，建议开启gps定位权限";
                break;
        }
        return str;
    }
}
